defmodule Coffer.Definition do
  defmacro __using__(_) do
    quote do
      def store(original_path) do
        storage = Application.get_env(:coffer, :storage)
        storage.store(__MODULE__, %Coffer.File{original_path: original_path})
      end

      def versions, do: [:original]
      def transform(_version, _file), do: :noaction
      def storage_dir(_version, _file), do: Application.get_env(:coffer, :storage_dir)
      def filename(_version, file) do
        :crypto.strong_rand_bytes(20)
        |> Base.encode32()
        |> Kernel.<>(Path.extname(file.original_path))
      end

      defoverridable [
        versions: 0,
        transform: 2,
        storage_dir: 2,
        filename: 2
      ]
    end
  end
end
